{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Four Demos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# use pip (or, conda) to make sure numpy is installed:\n",
    "!pip -q install numpy  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from mpyc.runtime import mpc\n",
    "import secretsanta, id3gini, lpsolver, cnnmnist"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Default runs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Setting input to default = 8\n",
      "2020-07-18 10:43:57,868 Start MPyC runtime v0.6.8\n",
      "Using secure integers: SecInt32\n",
      "2 [1, 0]\n",
      "3 [2, 0, 1]\n",
      "4 [2, 3, 0, 1]\n",
      "5 [1, 2, 0, 4, 3]\n",
      "6 [2, 3, 0, 1, 5, 4]\n",
      "7 [4, 3, 1, 0, 6, 2, 5]\n",
      "8 [7, 3, 4, 1, 0, 2, 5, 6]\n",
      "Using secure fixed-point numbers: SecFxp32:16\n",
      "2 [1.0, 0.0]\n",
      "3 [1.0, 2.0, 0.0]\n",
      "4 [2.0, 3.0, 1.0, 0.0]\n",
      "5 [4.0, 0.0, 1.0, 2.0, 3.0]\n",
      "6 [1.0, 4.0, 3.0, 0.0, 5.0, 2.0]\n",
      "7 [2.0, 5.0, 6.0, 0.0, 1.0, 4.0, 3.0]\n",
      "8 [3.0, 4.0, 7.0, 2.0, 6.0, 0.0, 5.0, 1.0]\n",
      "Using secure prime fields: SecFld3(GF(11))\n",
      "2 [1, 0]\n",
      "3 [1, 2, 0]\n",
      "4 [1, 2, 3, 0]\n",
      "5 [1, 0, 4, 2, 3]\n",
      "6 [2, 3, 1, 0, 5, 4]\n",
      "7 [2, 6, 3, 4, 1, 0, 5]\n",
      "8 [6, 5, 7, 1, 2, 0, 3, 4]\n",
      "Using secure binary fields: SecFld3(GF(2^3))\n",
      "2 [1, 0]\n",
      "3 [1, x, 0]\n",
      "4 [x+1, x, 1, 0]\n",
      "5 [1, 0, x^2, x, x+1]\n",
      "6 [x, x+1, x^2+1, x^2, 0, 1]\n",
      "7 [x+1, x^2+x, 0, x^2+1, 1, x^2, x]\n",
      "8 [x^2+x, x^2+x+1, x^2+1, 1, 0, x, x+1, x^2]\n",
      "Using secure quinary fields: SecFld4(GF(5^2))\n",
      "2 [1, 0]\n",
      "3 [2, 0, 1]\n",
      "4 [2, 3, 0, 1]\n",
      "5 [4, 0, 3, 1, 2]\n",
      "6 [x, 2, 4, 1, 3, 0]\n",
      "7 [3, x+1, 0, 4, 2, 1, x]\n",
      "8 [x+1, x, 4, 2, 0, x+2, 3, 1]\n",
      "Using secure extension fields (medium prime): SecFld24(GF(11^7))\n",
      "2 [1, 0]\n",
      "3 [1, 2, 0]\n",
      "4 [1, 0, 3, 2]\n",
      "5 [2, 3, 1, 4, 0]\n",
      "6 [4, 0, 3, 2, 5, 1]\n",
      "7 [6, 0, 1, 2, 3, 4, 5]\n",
      "8 [5, 6, 1, 0, 2, 7, 4, 3]\n",
      "Using secure extension fields (larger prime): SecFld30(GF(1031^3))\n",
      "2 [1, 0]\n",
      "3 [2, 0, 1]\n",
      "4 [2, 3, 1, 0]\n",
      "5 [1, 3, 4, 2, 0]\n",
      "6 [2, 4, 1, 5, 3, 0]\n",
      "7 [6, 3, 4, 0, 2, 1, 5]\n",
      "8 [1, 4, 5, 2, 6, 7, 0, 3]\n",
      "2020-07-18 10:43:58,509 Stop MPyC runtime -- elapsed time: 0:00:00.637958\n"
     ]
    }
   ],
   "source": [
    "mpc.run(secretsanta.main())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using secure integers: SecInt32\n",
      "dataset: tennis with 14 samples and 4 attributes\n",
      "2020-07-18 10:43:58,529 Start MPyC runtime v0.6.8\n",
      "2020-07-18 10:43:58,537 Attribute node 0\n",
      "2020-07-18 10:43:58,540 Leaf node label 1\n",
      "2020-07-18 10:43:58,548 Attribute node 3\n",
      "2020-07-18 10:43:58,551 Leaf node label 0\n",
      "2020-07-18 10:43:58,554 Leaf node label 1\n",
      "2020-07-18 10:43:58,562 Attribute node 2\n",
      "2020-07-18 10:43:58,565 Leaf node label 0\n",
      "2020-07-18 10:43:58,568 Leaf node label 1\n",
      "2020-07-18 10:43:58,568 Stop MPyC runtime -- elapsed time: 0:00:00.037977\n",
      "Decision tree of depth 2 and size 8: \n",
      "if Outlook == Overcast: Yes\n",
      "if Outlook == Rain: \n",
      "|   if Wind == Strong: No\n",
      "|   if Wind == Weak: Yes\n",
      "if Outlook == Sunny: \n",
      "|   if Humidity == High: No\n",
      "|   if Humidity == Normal: Yes\n"
     ]
    }
   ],
   "source": [
    "mpc.run(id3gini.main())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using secure 8-bit integers: SecInt8\n",
      "dataset: uvlp with 2 constraints and 3 variables (scale factor 1)\n",
      "2020-07-18 10:43:58,576 Start MPyC runtime v0.6.8\n",
      "2020-07-18 10:43:58,581 Iteration 1/2: 0.0 pivot=1.0\n",
      "2020-07-18 10:43:58,586 Iteration 2/2: 10.0 pivot=3.0\n",
      "max = 37 / 3 / 1 = 12.333333333333334 in 2 iterations\n",
      "2020-07-18 10:43:58,590 Solution x\n",
      "2020-07-18 10:43:58,594 Dual solution y\n",
      "verification c.x == y.b, A.x <= b, x >= 0, y.A <= c, y <= 0: True\n",
      "solution = [1.3333333333333333, 0.3333333333333333, 0.0]\n",
      "2020-07-18 10:43:58,598 Stop MPyC runtime -- elapsed time: 0:00:00.020999\n"
     ]
    }
   ],
   "source": [
    "mpc.run(lpsolver.main())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-07-18 10:43:58,608 Start MPyC runtime v0.6.8\n",
      "2020-07-18 10:43:58,608 --------------- INPUT   -------------\n",
      "Type = SecInt37, range = (1337, 1338)\n",
      "Labels: [2]\n",
      "[[0000000000000000000000000000]\n",
      " [0000000000000000000000000000]\n",
      " [0000000011111000000000000000]\n",
      " [0000000011111100000000000000]\n",
      " [0000000111111110000000000000]\n",
      " [0000000011111110000000000000]\n",
      " [0000000000011110000000000000]\n",
      " [0000000000011110000000000000]\n",
      " [0000000000011110000000000000]\n",
      " [0000000000111110000000000000]\n",
      " [0000000000111110000000000000]\n",
      " [0000000000111110000000000000]\n",
      " [0000000001111100000000000000]\n",
      " [0000000001111100000000000000]\n",
      " [0000000011111000000000000000]\n",
      " [0000000011111000000000000000]\n",
      " [0000000011110000000001110000]\n",
      " [0000000011111000111111111000]\n",
      " [0000000011111111111111111000]\n",
      " [0000000011111111111111111000]\n",
      " [0000000001111111111111110000]\n",
      " [0000000000111111111000000000]\n",
      " [0000000000000000000000000000]\n",
      " [0000000000000000000000000000]\n",
      " [0000000000000000000000000000]\n",
      " [0000000000000000000000000000]\n",
      " [0000000000000000000000000000]\n",
      " [0000000000000000000000000000]]\n",
      "2020-07-18 10:43:58,653 --------------- LAYER 1 -------------\n",
      "2020-07-18 10:43:58,654 - - - - - - - - conv2d  - - - - - - -\n",
      "2020-07-18 10:43:58,975 Barrier 0 0\n",
      "2020-07-18 10:43:58,975 - - - - - - - - maxpool - - - - - - -\n",
      "2020-07-18 10:44:07,942 Barrier 0 0\n",
      "2020-07-18 10:44:07,942 - - - - - - - - ReLU    - - - - - - -\n",
      "2020-07-18 10:44:10,912 Barrier 0 0\n",
      "2020-07-18 10:44:10,912 --------------- LAYER 2 -------------\n",
      "2020-07-18 10:44:11,002 - - - - - - - - conv2d  - - - - - - -\n",
      "2020-07-18 10:44:11,916 Barrier 1 0\n",
      "2020-07-18 10:44:12,772 Barrier 1 0\n",
      "2020-07-18 10:44:13,619 Barrier 1 0\n",
      "2020-07-18 10:44:14,465 Barrier 1 0\n",
      "2020-07-18 10:44:14,602 Barrier 0 0\n",
      "2020-07-18 10:44:14,602 - - - - - - - - maxpool - - - - - - -\n",
      "2020-07-18 10:44:19,809 Barrier 0 0\n",
      "2020-07-18 10:44:19,809 - - - - - - - - ReLU    - - - - - - -\n",
      "2020-07-18 10:44:21,548 Barrier 0 0\n",
      "2020-07-18 10:44:21,548 --------------- LAYER 3 -------------\n",
      "2020-07-18 10:44:26,691 - - - - - - - - fc      - - - - - - -\n",
      "2020-07-18 10:44:29,425 - - - - - - - - ReLU    - - - - - - -\n",
      "2020-07-18 10:44:30,078 Barrier 0 0\n",
      "2020-07-18 10:44:30,078 --------------- LAYER 4 -------------\n",
      "2020-07-18 10:44:30,298 - - - - - - - - fc      - - - - - - -\n",
      "2020-07-18 10:44:30,314 --------------- OUTPUT  -------------\n",
      "Image #1337 with label 2: 2 predicted. \n",
      "[803671699, -820964441, 18182689095, -7857483958, 450677317, -9874255004, 7012023464, -9834985682, -6607493709, -14607362599]\n",
      "2020-07-18 10:44:30,329 Stop MPyC runtime -- elapsed time: 0:00:31.721460\n"
     ]
    }
   ],
   "source": [
    "mpc.run(cnnmnist.main()) # takes a minute or so"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Runs with specific inputs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using secure integers: SecInt32\n",
      "2 [1, 0]\n",
      "3 [1, 2, 0]\n",
      "4 [3, 2, 1, 0]\n",
      "5 [2, 4, 0, 1, 3]\n",
      "6 [2, 3, 4, 5, 1, 0]\n",
      "Using secure fixed-point numbers: SecFxp32:16\n",
      "2 [1.0, 0.0]\n",
      "3 [1.0, 2.0, 0.0]\n",
      "4 [1.0, 3.0, 0.0, 2.0]\n",
      "5 [3.0, 2.0, 4.0, 0.0, 1.0]\n",
      "6 [2.0, 3.0, 4.0, 5.0, 0.0, 1.0]\n",
      "Using secure prime fields: SecFld2(GF(7))\n",
      "2 [1, 0]\n",
      "3 [1, 2, 0]\n",
      "4 [2, 3, 1, 0]\n",
      "5 [2, 3, 4, 1, 0]\n",
      "6 [3, 2, 4, 1, 5, 0]\n",
      "Using secure binary fields: SecFld3(GF(2^3))\n",
      "2 [1, 0]\n",
      "3 [1, x, 0]\n",
      "4 [1, x, x+1, 0]\n",
      "5 [x+1, x, 1, x^2, 0]\n",
      "6 [x, x^2, 1, x^2+1, x+1, 0]\n",
      "Using secure quinary fields: SecFld4(GF(5^2))\n",
      "2 [1, 0]\n",
      "3 [1, 2, 0]\n",
      "4 [2, 3, 1, 0]\n",
      "5 [3, 0, 4, 1, 2]\n",
      "6 [1, 4, 3, 2, x, 0]\n",
      "Using secure extension fields (medium prime): SecFld24(GF(11^7))\n",
      "2 [1, 0]\n",
      "3 [2, 0, 1]\n",
      "4 [3, 0, 1, 2]\n",
      "5 [3, 4, 0, 2, 1]\n",
      "6 [5, 4, 3, 2, 1, 0]\n",
      "Using secure extension fields (larger prime): SecFld30(GF(1031^3))\n",
      "2 [1, 0]\n",
      "3 [1, 2, 0]\n",
      "4 [2, 0, 3, 1]\n",
      "5 [2, 0, 3, 4, 1]\n",
      "6 [4, 5, 3, 2, 1, 0]\n",
      "\n",
      "Using secure integers: SecInt95\n",
      "dataset: car with 1728 samples and 6 attributes\n",
      "Decision tree of depth 3 and size 25: \n",
      "if safety == high: \n",
      "|   if persons == 2: unacc\n",
      "|   if persons == 4: \n",
      "|   |   if buying == high: acc\n",
      "|   |   if buying == low: acc\n",
      "|   |   if buying == med: acc\n",
      "|   |   if buying == vhigh: acc\n",
      "|   if persons == more: \n",
      "|   |   if buying == high: acc\n",
      "|   |   if buying == low: vgood\n",
      "|   |   if buying == med: acc\n",
      "|   |   if buying == vhigh: unacc\n",
      "if safety == low: unacc\n",
      "if safety == med: \n",
      "|   if persons == 2: unacc\n",
      "|   if persons == 4: \n",
      "|   |   if buying == high: unacc\n",
      "|   |   if buying == low: acc\n",
      "|   |   if buying == med: acc\n",
      "|   |   if buying == vhigh: unacc\n",
      "|   if persons == more: \n",
      "|   |   if lug_boot == big: acc\n",
      "|   |   if lug_boot == med: acc\n",
      "|   |   if lug_boot == small: unacc\n",
      "\n",
      "Using secure 6-bit integers: SecInt6\n",
      "dataset: wiki with 2 constraints and 3 variables (scale factor 1)\n",
      "max = 60 / 3 / 1 = 20.0 in 1 iterations\n",
      "verification c.x == y.b, A.x <= b, x >= 0, y.A <= c, y <= 0: True\n",
      "solution = [0.0, 0.0, 5.0]\n",
      "\n",
      "Type = SecFxp10:4, range = (5095, 5096)\n",
      "Labels: [2]\n",
      "[[0000000000000000000000000000]\n",
      " [0000000000000000000000000000]\n",
      " [0000000000000000000000000000]\n",
      " [0000000000000000000000000000]\n",
      " [0000000000000000110000000000]\n",
      " [0000000000001111111100000000]\n",
      " [0000000000111111111110000000]\n",
      " [0000000001111111111111000000]\n",
      " [0000000001111111111111000000]\n",
      " [0000000001111111111111100000]\n",
      " [0000000001111110011111100000]\n",
      " [0000000000000000011111100000]\n",
      " [0000000000000000011111000000]\n",
      " [0000000000000000011110000000]\n",
      " [0000000000000000111110000000]\n",
      " [0000000000011111111100000000]\n",
      " [0000001111111111111111100000]\n",
      " [0000011111111111111111110000]\n",
      " [0000011111111111111111111000]\n",
      " [0000011111111111111111111000]\n",
      " [0000011111111110000000111000]\n",
      " [0000011111111100000000000000]\n",
      " [0000011111110000000000000000]\n",
      " [0000001111000000000000000000]\n",
      " [0000000000000000000000000000]\n",
      " [0000000000000000000000000000]\n",
      " [0000000000000000000000000000]\n",
      " [0000000000000000000000000000]]\n",
      "Image #5095 with label 2: 2 predicted. \n",
      "[-4.125, -3.1875, 18.625, 0.25, -4.75, -10.3125, -15.0625, 1.5, -1.25, -6.5]\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "mpc.logging(False)\n",
    "\n",
    "sys.argv[1:] = ['6']\n",
    "mpc.run(secretsanta.main())\n",
    "\n",
    "print()\n",
    "\n",
    "sys.argv[1:] = ['-i', '2']\n",
    "mpc.run(id3gini.main())\n",
    "\n",
    "print()\n",
    "\n",
    "sys.argv[1:] = ['-i', '1']\n",
    "mpc.run(lpsolver.main())\n",
    "\n",
    "print()\n",
    "\n",
    "sys.argv[1:] = ['1.5']  # forces use of secure fixed-point arithmetic\n",
    "mpc.run(cnnmnist.main())     # takes a minute or so\n",
    "\n",
    "sys.argv[1:] = []"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
